package run;

import java.sql.*;
import database.*;
import eb.*;
import java.io.*;

public class Register {
	private Connection conn=null;
	public boolean AddUser(User UserInfo){
		try{
			SetConn testconn=new SetConn();
			conn=testconn.setupConn();
			String CheckSql="select * from member where UserName= ?";
			ResultSet rs;
			PreparedStatement queryPs=conn.prepareStatement(CheckSql);
			queryPs.setString(1,UserInfo.GetUserName());
			rs=queryPs.executeQuery();
			if(rs.next()){
				System.out.println("this user name has been existed\n");
				return false;
			}
			else{
				System.out.println("this user name has not existed\n");
				int result;
				String sql="insert into member(UserName,Password,RealName,Sex,Tel,Email,Address,Postcode) values(?,?,?,?,?,?,?,?)";
				String sex=UserInfo.GetSex();
				int sexenum;
				if(sex.equals("M")){
					sexenum=0;
				}
				else 
					sexenum=1;
				PreparedStatement insertmember=conn.prepareStatement(sql);
				insertmember.setString(1,UserInfo.GetUserName());
				insertmember.setString(2, UserInfo.GetPassWord());
				insertmember.setString(3, UserInfo.GetRealName());
				insertmember.setString(4, "M");
				insertmember.setString(5, UserInfo.GetTel());
				insertmember.setString(6,UserInfo.GetEmail());
				insertmember.setString(7, UserInfo.GetAddress());
				insertmember.setString(8, UserInfo.GetPostcode());
				result=insertmember.executeUpdate();
				
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
}
